package com.orhon.smartcampus.modules.base.inputMapper;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.GQLLangInput;
import com.orhon.smartcampus.modules.base.gqlInputModel.GQLPeriodInput;
import com.orhon.smartcampus.basicComponents.ConvertHelper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Qualifier;
import org.mapstruct.factory.Mappers;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

@Mapper(uses = {
        ConvertHelper.class
})
public interface PeriodInputMapper {

    PeriodInputMapper MAPPER = Mappers.getMapper(PeriodInputMapper.class);

    default GQLPeriodInput mapInput2(final Map map){
        String json = JSON.toJSONString(map);
        GQLPeriodInput input = JSON.parseObject(json , GQLPeriodInput.class);
        return input;
    }


    /////////////// 其他实现方式,用于复杂需求  ////////////////////


    @Mapping(source = "map" , target = "id" , qualifiedBy = ConvertHelper.Id.class)
    @Mapping(expression = "java( (String)map.get(\"period_slug\") )" , target = "period_slug")
    @Mapping(expression = "java( (String)map.get(\"period_number\") )" , target = "period_number")
    @Mapping(expression = "java( (String)map.get(\"mark\") )" , target = "mark")
    @Mapping(source = "map" , target = "period_name" , qualifiedBy = LangInputPeriodName.class)
    GQLPeriodInput mapInput(final Map map);


    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.SOURCE)
    public @interface LangInputPeriodName {
    }

    @LangInputPeriodName
    default public GQLLangInput langInput(Map<String, Object> in){
        GQLLangInput langInput = new GQLLangInput();
        try{
            Map inMap = (Map) in.get("period_name");
            String json = JSON.toJSONString(inMap);
            GQLLangInput input = JSON.parseObject(json , GQLLangInput.class);
            return input;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
